#include "app/libc/std.h"
#include "app/libc/print.h"
#include "app/libc/type.h"
#include "app/libdraw/draw.h"

//__asm__ ("1:\n\t"
//         "mov $0x3f8, %dx\n\t"
//         "mov $'D', %ax\n\t"
//         "out %ax, %dx\n\t"
//         "mov $1000, %rdi\n\t"
//         "mov $0, %rax\n\t"
//         "syscall\n\t"
//         "jmp 1b");
//

void draw_char(uint64_t fbaddr, struct mode_info* minfo, int color) {
  // H
  draw_char_en(10, 2, 0, color, fbaddr, minfo);
  // e
  draw_char_en(20, 2, 1, color, fbaddr, minfo);
  // l
  draw_char_en(30, 2, 2, color, fbaddr, minfo);
  // l
  draw_char_en(40, 2, 2, color, fbaddr, minfo);
  // o
  draw_char_en(50, 2, 3, color, fbaddr, minfo);
  // ,
  draw_char_en(60, 2, 4, color, fbaddr, minfo);
  // zhong
  draw_char_zh(70, 2, 0, color, fbaddr, minfo);
  // guo
  draw_char_zh(90, 2, 1, color, fbaddr, minfo);
  // .
  draw_char_en(110, 2, 5, color, fbaddr, minfo);
}

/*
* Using app/libc/start.S (starting from virtual address 0x100000) invoke `call main` and jumps to this point
* Simulate packaging application app, including C library startup file to call the application main() function
*/
int main() {
  // draw
  struct mode_info mode_info;
  get_mode_info(&mode_info);
  uint64_t fbbase = fbmap();
  uint64_t cnt = 0;
  uint32_t colors[] = { RED | GREEN, RED | BLUE, GREEN | BLUE };

  while(1) {
    unsigned int* i = (unsigned int*)0x12345678; // invoke Page Fault, unknown address
    *i = 0x45;
    print(*i); // first 0x0, after 0x45

    void* shm = shm_open("shm-1");
    print(*(char*)shm); // read from shm

    draw_char(fbbase, &mode_info, colors[cnt++ % 3]);

    sleep(3000);
  }
}